package com.tuanzi.loan.business.service.family.impl;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.family.LargeAmountChannelManage;
import com.tuanzi.loan.business.entity.family.LoanChannelNdInfo;
import com.tuanzi.loan.business.repository.family.LargeAmountChannelManageRepository;
import com.tuanzi.loan.business.service.family.LargeAmountChannelManageService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.LargeAmountChannelManageSearchRequest;
import com.tuanzi.loan.vo.response.family.LargeAmountChannelManageSearchResponse;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class LargeAmountChannelManageServiceImpl extends BaseAuditServiceImpl<LargeAmountChannelManage, Long> implements LargeAmountChannelManageService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    LargeAmountChannelManageRepository repository;

    @Autowired
    DictionaryService service;

    @Override
    protected JPAAuditRepository<LargeAmountChannelManage, Long> getRepository() {
        return null;
    }

    @Override
    public Page<LargeAmountChannelManageSearchResponse> list(PageSearcher<LargeAmountChannelManageSearchRequest> pageSearcher) {
        return repository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }
    /*增加对象*/
    @Override
    public LargeAmountChannelManage saveLargeAmountChannelManage(LargeAmountChannelManageSearchRequest request) {
        deleteRedis();
        LargeAmountChannelManage largeAmountChannelManage = new LargeAmountChannelManage();
        largeAmountChannelManage.setAmountInterval(request.getAmountInterval());
        largeAmountChannelManage.setAppChannel(request.getAppChannel());
        largeAmountChannelManage.setChannelName(request.getChannelName());
        largeAmountChannelManage.setMaxAmount(request.getMaxAmount());
        largeAmountChannelManage.setMinAmount(request.getMinAmount());
        largeAmountChannelManage.setPrdId(request.getPrdId());
        largeAmountChannelManage.setCreator(CurrentUser.get().getName());
        largeAmountChannelManage.setCreateTime(Timestamp.valueOf(DateUtils.getCurrentTime()));
        largeAmountChannelManage.setProductName(request.getProductName());
        return repository.saveAndFlush(largeAmountChannelManage);
    }

    /*删除配置*/
    @Override
    public void deleteByid(LargeAmountChannelManageSearchResponse response) {
        try {
            deleteRedis();
            repository.delete((Long) response.getId());
        } catch (Exception e) {
            LogService.error("删除失败",e);
        }
    }

    @Override
    public Integer updateLargeAmountChannelManage(LargeAmountChannelManageSearchRequest request) {
        deleteRedis();
        return repository.updateByEntity(request);
    }

    @Override
    public List<String> selectDistinctCFrom() {
        return repository.selectChannelInfo();
    }


    @Override
    public List<CommProductChannel> getChannelInfo(Integer prdid) {
        return repository.getChannelInfo(prdid);
    }

    @Override
    public Integer addChannelND(LoanChannelNdInfo info) {
        return repository.addChannelND(info);
    }

    @Override
    public void addBatchChannelNd() {
        repository.addBatchChannelNd();
    }

    /**
     * 删除渠道配置缓存
     */
    void deleteRedis(){
        service.listMapByCode(DictionaryCode.PRODUCT_MONEY).forEach((k, v)->{
            stringRedisTemplate.delete(String.format(LoanRedisKey.LOAN_LARGE_AMOUNT_CHANNEL_INFO,Integer.parseInt(k)));
        });
    };
}
